package com.aki.ai_property.service.impl;

import com.aki.ai_property.dao.HouseholderDao;
import com.aki.ai_property.dto.HouseholderSexTjDto;
import com.aki.ai_property.entity.Householder;
import com.aki.ai_property.service.intf.HouseholderService;
import com.aki.ai_property.util.StringUtil;
import com.aki.ai_property.vo.PageBean;
import com.aki.ai_property.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * ━━━━━━永无BUG!━━━━━━
 *
 * @author AKI
 * @date 2022/8/30
 */
@Service
public class HouseholderServiceImpl implements HouseholderService {
    @Autowired
    private HouseholderDao dao;
    @Override
    public Result save(Householder householder) {
        if(dao.save(householder)>0){
            return Result.ok();
        }
        return Result.fail("添加失败！");
    }

    @Override
    public Result update(Householder householder) {
        if(dao.update(householder)>0){
            return Result.ok();
        }
        return Result.fail();
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {
        if(StringUtil.isNotEmpty(name)){
            name = "%" + name +"%";
        }
        int start = (page-1)*limit;
        return PageBean.ok(dao.selectCount(name),dao.selectPage(name,start,limit));
    }

    /** 查询全部 */
    @Override
    public Result all() {
        return Result.ok(dao.all());
    }

    @Override
    public Result sextj() {

        //1.查询数据
        List<Householder> list=dao.selectSex();
        List<HouseholderSexTjDto> result=new ArrayList<>();
        //2.组装数据  JDk8新特性
        list.forEach(s->{
            result.add(new HouseholderSexTjDto(s.getSex(),s.getNum()));
        });
        //3.返回数据
        return Result.ok(result);


    }



}
